<template>
  <div>
    <div class="app-container"  v-if="opendetail == false">
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="ID" prop="recordId">
          <el-input
              v-model="queryParams.orderId"
              placeholder="请输入ID"
              clearable
              @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="订单编号" prop="orderNumber">
          <el-input
              v-model="queryParams.orderNumber"
              placeholder="请输入订单编号"
              clearable
              @keyup.enter="handleQuery"
          />
        </el-form-item>


        <el-form-item label="下单时间" style="width: 308px">
          <el-date-picker
              v-model="daterangeOrderTime"
              value-format="YYYY-MM-DD"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="预约时间" style="width: 308px">
          <el-date-picker
              v-model="daterangeAppointmentTime"
              value-format="YYYY-MM-DD"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>



      </el-form>

      <el-row :gutter="10" class="mb8">

        <el-col :span="1.5">
          <el-button
              type="warning"
              plain
              icon="Download"
              @click="handleExport"
              v-hasPermi="['masterrabbit-kaixuan:orderrecords:export']"
          >导出</el-button>
        </el-col>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="orderrecordsList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="ID" align="center" prop="orderId" />
        <el-table-column label="订单编号" align="center" prop="orderNumber" />
        <el-table-column label="商家名" align="center" prop="merchantName" />
        <el-table-column label="服务分类" align="center" prop="serviceCategory">
          <template #default="scope">
            <dict-tag :options="sys_service_type" :value="parseInt(scope.row.serviceCategory)"/>
          </template>
        </el-table-column>
        <el-table-column label="下单时间" align="center" prop="orderTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.orderTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="预约时间" align="center" prop="appointmentTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.appointmentTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="车牌" align="center" prop="userVehicles.licensePlate" />
        <el-table-column label="订单状态" align="center" prop="status" >
          <template #default="scope">
            <dict-tag :options="sys_orders_state" :value="scope.row.status"/>
          </template>
        </el-table-column>
        <el-table-column label="订单金额" align="center" prop="totalAmount" />
<!--        <el-table-column label="优惠金额" align="center" prop="discountAmount" />-->
        <el-table-column label="实际金额" align="center" prop="paidAmount" />
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <!--          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['masterrabbit-kaixuan:orderrecords:edit']">修改</el-button>-->
            <!--          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['masterrabbit-kaixuan:orderrecords:remove']">删除</el-button>-->
            <el-button link type="primary" icon="Delete" @click="getdetail(scope.row)" >详情</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
          v-show="total>0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
      />

      <!-- 添加或修改订单记录对话框 -->
      <!--    <el-dialog :title="title" v-model="open" width="500px" append-to-body>-->
      <!--      <el-form ref="orderrecordsRef" :model="form" :rules="rules" label-width="80px">-->
      <!--        <el-form-item label="订单编号" prop="orderNumber">-->
      <!--          <el-input v-model="form.orderNumber" placeholder="请输入订单编号" />-->
      <!--        </el-form-item>-->
      <!--        <el-form-item label="服务分类" prop="serviceCategory">-->
      <!--          <el-input v-model="form.serviceCategory" placeholder="请输入服务分类" />-->
      <!--        </el-form-item>-->
      <!--        <el-form-item label="服务门店" prop="serviceShop">-->
      <!--          <el-input v-model="form.serviceShop" placeholder="请输入服务门店" />-->
      <!--        </el-form-item>-->
      <!--        <el-form-item label="下单时间" prop="orderTime">-->
      <!--          <el-date-picker clearable-->
      <!--            v-model="form.orderTime"-->
      <!--            type="date"-->
      <!--            value-format="YYYY-MM-DD"-->
      <!--            placeholder="请选择下单时间">-->
      <!--          </el-date-picker>-->
      <!--        </el-form-item>-->
      <!--        <el-form-item label="预约时间" prop="appointmentTime">-->
      <!--          <el-date-picker clearable-->
      <!--            v-model="form.appointmentTime"-->
      <!--            type="date"-->
      <!--            value-format="YYYY-MM-DD"-->
      <!--            placeholder="请选择预约时间">-->
      <!--          </el-date-picker>-->
      <!--        </el-form-item>-->
      <!--        <el-form-item label="车辆" prop="vehicleInfo">-->
      <!--          <el-input v-model="form.vehicleInfo" placeholder="请输入车辆" />-->
      <!--        </el-form-item>-->
      <!--        <el-form-item label="备注" prop="remarks">-->
      <!--          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />-->
      <!--        </el-form-item>-->
      <!--        <el-form-item label="服务项目" prop="serviceName">-->
      <!--          <el-input v-model="form.serviceName" placeholder="请输入服务项目" />-->
      <!--        </el-form-item>-->
      <!--        <el-form-item label="订单金额" prop="totalAmount">-->
      <!--          <el-input v-model="form.totalAmount" placeholder="请输入订单金额" />-->
      <!--        </el-form-item>-->
      <!--        <el-form-item label="优惠金额" prop="discountAmount">-->
      <!--          <el-input v-model="form.discountAmount" placeholder="请输入优惠金额" />-->
      <!--        </el-form-item>-->
      <!--        <el-form-item label="实际金额" prop="actualAmount">-->
      <!--          <el-input v-model="form.actualAmount" placeholder="请输入实际金额" />-->
      <!--        </el-form-item>-->
      <!--      </el-form>-->
      <!--      <template #footer>-->
      <!--        <div class="dialog-footer">-->
      <!--          <el-button type="primary" @click="submitForm">确 定</el-button>-->
      <!--          <el-button @click="cancel">取 消</el-button>-->
      <!--        </div>-->
      <!--      </template>-->
      <!--    </el-dialog>-->
    </div>
    <div v-if="opendetail == true">
      <el-form>
        <el-button type="primary" @click="backlast">返回</el-button>
      </el-form>
      <el-card class="box-card">
        <el-form label-width="120px" label-position="left">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="订单编号">
                <span>{{ details.orderNumber }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="订单ID">
                <span>{{ details.orderId }}</span>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="服务类型">
                  <dict-tag :options="sys_service_type" :value="parseInt(details.serviceCategory)"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="订单状态">
                  <dict-tag :options="sys_orders_state" :value="details.status"/>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="商铺名称">
                <span>{{ details.merchantName }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="车牌号">
<!--                <span>{{ details.userVehicles.licensePlate }}</span>-->
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="订单金额">
                <span>{{ details.totalAmount }}</span>
              </el-form-item>
            </el-col>


            <el-col :span="12">
              <el-form-item label="实际金额">
                <span>{{ details.paidAmount }}</span>
              </el-form-item>
            </el-col>
          </el-row>

          <el-form-item label="下单时间">
            <span>{{ details.orderTime }}</span>
          </el-form-item>
          <el-form-item label="预约时间">
            <span>{{ details.appointmentTime }}</span>
          </el-form-item>
          <el-form-item label="备注">
            <span>{{ details.remark }}</span>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script setup name="Orderrecords">
import { listOrders, getOrders, delOrders, addOrders, updateOrders, } from "@/api/masterrabbit-kaixuan/orders";

const { proxy } = getCurrentInstance();

const { sys_service_type } = proxy.useDict('sys_service_type');
const { sys_orders_state } = proxy.useDict('sys_orders_state');

const orderrecordsList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeOrderTime = ref([]);
const daterangeAppointmentTime = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderId: null,
    orderNumber: null,
    merchantId:null,
    vehicleId: null,
    coustomerId:null,
    orderTime: null,
    appointmentTime: null,
    remarks: null,
    status: null,
    couponCode: null,
    totalAmount: null,
    paidAmount: null,
    meerchantName: null,
    userVehicles:null,
    serviceCategory: 3,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询订单记录列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeOrderTime && '' != daterangeOrderTime) {
    queryParams.value.params["beginOrderTime"] = daterangeOrderTime.value[0];
    queryParams.value.params["endOrderTime"] = daterangeOrderTime.value[1];
  }
  if (null != daterangeAppointmentTime && '' != daterangeAppointmentTime) {
    queryParams.value.params["beginAppointmentTime"] = daterangeAppointmentTime.value[0];
    queryParams.value.params["endAppointmentTime"] = daterangeAppointmentTime.value[1];
  }
  listOrders(queryParams.value).then(response => {
    console.log(response)
    orderrecordsList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    orderId: null,
    orderNumber: null,
    merchantId:null,
    vehicleId: null,
    coustomerId:null,
    orderTime: null,
    appointmentTime: null,
    remarks: null,
    status: null,
    couponCode: null,
    totalAmount: null,
    paidAmount: null,
    meerchantName: null,
    serviceCategory: 3,
  };
  proxy.resetForm("orderrecordsRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeOrderTime.value = [];
  daterangeAppointmentTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.recordId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加订单记录";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _recordId = row.recordId || ids.value
  getOrders(_recordId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改订单记录";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["orderrecordsRef"].validate(valid => {
    if (valid) {
      if (form.value.recordId != null) {
        updateOrders(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addOrders(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _recordIds = row.recordId || ids.value;
  proxy.$modal.confirm('是否确认删除订单记录编号为"' + _recordIds + '"的数据项？').then(function () {
    return delOrders(_recordIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('masterrabbit-kaixuan/orderrecords/export', {
    ...queryParams.value
  }, `orderrecords_${new Date().getTime()}.xlsx`)
}

/** 打开/退出 详情页 */
const opendetail = ref(false)
const details = ref({})

function getdetail(row) {
  opendetail.value = true
  console.log(row)
  details.value = row
}

function backlast() {
  opendetail.value = false
}


getList();
</script>
<style scoped>
.box-card {
  max-width: 800px;
  margin: 20px auto;
  padding: 20px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  border-radius: 8px;
}

.el-form-item {
  margin-bottom: 16px;
}
</style>
